package com.ecole.mines.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;

import android.os.AsyncTask;
import android.os.Environment;

public class FTP {

	private String host = "91.121.167.164";
	private int port = 21;
	private String user = "jpc";
	private String pass = "bijou30!";
	private String errorDescription = "";

	// filepath est le r�pertoire o� sont contenus les fichiers � envoyer
	public boolean send(String filePath, File file) {
		System.out.println("D�but send");
		FTPClient client = new FTPClient();
		FileInputStream fis = null;

		try {
			client.connect("91.121.167.164", 21);
			boolean success = client.login(user, pass);
			if (!success) {
				errorDescription = "Connexion au serveur FTP impossible";
				return false;
			}
			// Création des répertoires 
			makeDir("Emargement");
			File f = new File(file.getParent());
			makeDir(f.getParent());
			makeDir(file.getParent());
			
			
			String filename = filePath;
			fis = new FileInputStream(filename);
			System.out.println(filename);
			client.storeFile(file.getName(), fis);
			client.logout();
			client.disconnect();
			return true;
		} catch (IOException e) {
			errorDescription = "Oops! Une erreur est survenue :"
					+ e.getMessage();
			System.out.println(e.getMessage());
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				// client.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public boolean makeDir(String directory) {
		FTPClient client = new FTPClient();
		try {
			client.connect(host, port);
			boolean success = client.login(user, pass);
			if (!success) {
				errorDescription = "Connexion au serveur FTP impossible";
				return false;
			}

			// Creates a directory
			success = client.makeDirectory(directory);
			if (success) {
				return true;
			} else {
				errorDescription = "Impossible de cr�er le r�pertoire sur le serveur";
				return false;
			}
		} catch (IOException ex) {
			errorDescription = "Oops! Une erreur est survenue : "
					+ ex.getMessage();
			ex.printStackTrace();
		}
		return false;
	}
	
	public static void uploadDirectory(FTPClient ftpClient,
	        String remoteDirPath, String localParentDir, String remoteParentDir)
	        throws IOException {
	 
	    System.out.println("LISTING directory: " + localParentDir);
	 
	    File localDir = new File(localParentDir);
	    File[] subFiles = localDir.listFiles();
	    if (subFiles != null && subFiles.length > 0) {
	        for (File item : subFiles) {
	            String remoteFilePath = remoteDirPath + "/" + remoteParentDir
	                    + "/" + item.getName();
	            if (remoteParentDir.equals("")) {
	                remoteFilePath = remoteDirPath + "/" + item.getName();
	            }
	 
	 
	            if (item.isFile()) {
	                // upload the file
	                String localFilePath = item.getAbsolutePath();
	                System.out.println("About to upload the file: " + localFilePath);
	                boolean uploaded = uploadSingleFile(ftpClient,
	                        localFilePath, remoteFilePath);
	                if (uploaded) {
	                    System.out.println("UPLOADED a file to: "
	                            + remoteFilePath);
	                } else {
	                    System.out.println("COULD NOT upload the file: "
	                            + localFilePath);
	                }
	            } else {
	                // create directory on the server
	                boolean created = ftpClient.makeDirectory(remoteFilePath);
	                if (created) {
	                    System.out.println("CREATED the directory: "
	                            + remoteFilePath);
	                } else {
	                    System.out.println("COULD NOT create the directory: "
	                            + remoteFilePath);
	                }
	 
	                // upload the sub directory
	                String parent = remoteParentDir + "/" + item.getName();
	                if (remoteParentDir.equals("")) {
	                    parent = item.getName();
	                }
	 
	                localParentDir = item.getAbsolutePath();
	                uploadDirectory(ftpClient, remoteDirPath, localParentDir,
	                        parent);
	            }
	        }
	    }
	}
	
	public static boolean uploadSingleFile(FTPClient ftpClient,
	        String localFilePath, String remoteFilePath) throws IOException {
	    File localFile = new File(localFilePath);
	 
	    InputStream inputStream = new FileInputStream(localFile);
	    try {
	        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
	        return ftpClient.storeFile(remoteFilePath, inputStream);
	    } finally {
	        inputStream.close();
	    }
	}
	
	

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getErrorDescription() {
		return errorDescription;
	}
}
